package cn.myfirefly.core.filter;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

import cn.myfirefly.core.Const;
import cn.myfirefly.core.model.Permission;
import cn.myfirefly.core.model.ResultDto;
import cn.myfirefly.utils.PropUtil;
import cn.myfirefly.utils.StrUtil;
import cn.myfirefly.utils.WebUtil;

public class DefaultMyFireflyFilter extends MyFireflyFilter {

	private String token = null;
	
	private String apiServer = PropUtil.use(Const.Config.CONFIG_FILE).get(Const.Config.API_SERVER);
	
	private String secretKey = PropUtil.use(Const.Config.CONFIG_FILE).get(Const.Config.SECRET_KEY);
	
	private String systemId = PropUtil.use(Const.Config.CONFIG_FILE).get(Const.Config.SYSTEM_ID);
	
	public Map<String, Permission> getPermissions(String loginName) {
		if (token != null) {
			ResultDto resultDto = getPermission(systemId, loginName);
			if (StrUtil.equals(resultDto.getResultCode(), Const.ResultCode.SUCCESS)) {
				return (Map<String, Permission>)resultDto.getPermissions();
			} else {
				token = getToken(systemId);
			}
		} else {
			token = getToken(systemId);
		}
		
		ResultDto resultDto = getPermission(systemId, loginName);
		if (StrUtil.equals(resultDto.getResultCode(), Const.ResultCode.SUCCESS)) {
			return (Map<String, Permission>)resultDto.getPermissions();
		}
		return null;
	}
	
	public String getMenus(String loginName) {
		if (token != null) {
			String json = getMenu(systemId, loginName);
			JSONObject result = JSONObject.parseObject(json);
			if (StrUtil.equals(result.getString("resultCode"), Const.ResultCode.SUCCESS)) {
				return json;
			} else {
				token = getToken(systemId);
			}
		} else {
			token = getToken(systemId);
		}
		
		String json = getMenu(systemId, loginName);
		JSONObject result = JSONObject.parseObject(json);
		if (StrUtil.equals(result.getString("resultCode"), Const.ResultCode.SUCCESS)) {
			return json;
		}
		return null;
	}
	
	private String getToken(String systemId) {
		Map<String, String> queryParas = new HashMap<>();
		queryParas.put("systemId", systemId);
		queryParas.put("secretKey", secretKey);
		String json = WebUtil.post(apiServer+"/api/login.action", queryParas, null);
		ResultDto resultDto = JSONObject.parseObject(json, ResultDto.class);
		String token = null;
		if (StrUtil.equals(resultDto.getResultCode(), Const.ResultCode.SUCCESS)) {
			token = resultDto.getMessage();
		}
		return token;
	}
	
	private ResultDto getPermission(String systemId, String loginName) {
		Map<String, String> queryParas = new HashMap<>();
		queryParas.put("systemId", systemId);
		queryParas.put("loginName", loginName);
		queryParas.put("token", token);
		String json = WebUtil.post(apiServer+"/api/getPermissions.action", queryParas, null);
		return JSONObject.parseObject(json, ResultDto.class);
	}
	
	private String getMenu(String systemId, String loginName) {
		Map<String, String> queryParas = new HashMap<>();
		queryParas.put("systemId", systemId);
		queryParas.put("loginName", loginName);
		queryParas.put("token", token);
		return WebUtil.post(apiServer+"/api/getMenus.action", queryParas, null);
	}
}
